package org.ichow.scenegame.view
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import org.ichow.debug.Log;
	import org.ichow.scenegame.controller.NotiCommand;
	import org.ichow.scenegame.core.Item;
	import org.ichow.scenegame.core.NotiFactory;
	import org.ichow.scenegame.events.NotiEvent;
	import org.ichow.scenegame.events.SceneEvent;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * ...
	 * 操作层代理
	 * 控制动画播放完后显示的操作对象
	 * @author M.ichow
	 */
	public class NotiMediator extends Mediator
	{
		public static const NAME:String = "NotiMediator";
		//当前对象
		private var _currentNoti:DisplayObject;
		private var _currentStatus:String = NotiEvent.INIT;
		private var _continueInfo:String;
		private var _result:String
		
		public function NotiMediator( app:NotiFactory )
		{
			super( NAME , app );
		}
		
		/**
		 * 添加侦听
		 */
		override public function onRegister():void
		{
			super.onRegister();
			facade.registerCommand( NotiEvent.ADD_NOTI , NotiCommand );
			notiBox.addEventListener( NotiEvent.NOTI_CHANGE , onNotiChange );
			notiBox.addEventListener( NotiEvent.SELECT_ITEM , onSelectItem );
			notiBox.addEventListener( NotiEvent.TOOLTIP , onTooltip );
		}
		
		private function onTooltip( e:NotiEvent ):void
		{
			if ( e.data )
			{
				sendNotification( ToolTipMediator.SHOW_TOOLTIP , e.data , ToolTipMediator.NOTI_TOOLTIP );
			}
			else
			{
				sendNotification( ToolTipMediator.HIDE_TOOLTIP , null , ToolTipMediator.NOTI_TOOLTIP );
			}
		}
		
		private function onNotiChange( e:NotiEvent ):void
		{
			var status:String = e.status;
			switch ( status )
			{
				case NotiEvent.INIT: 
					sendNotification( NotiEvent.RESTART );
					sendNotification( SceneEvent.INIT_GAME );
					break;
				case NotiEvent.READY: 
					sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.READY );
					sendNotification( NotiEvent.SHOW_NOTI );
					break;
				case NotiEvent.PLAY: 
					sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.PLAY );
					sendNotification( NotiEvent.SHOW_NOTI );
					sendNotification( ToolsBarMediator.TOOLS_BAR , null , "show" );
					_result = "";
					break;
				case NotiEvent.CONTINUE: 
					if ( _result != "" ) {
						sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.END );
						sendNotification( SceneEvent.MOVIE_LOAD , _result );
					}
					else {
						sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.PLAY );
						sendNotification( NotiEvent.SHOW_NOTI );
					}
					break;
				case NotiEvent.END: 
					break;
				default: 
					break;
			}
		}
		
		/**
		 * 删除
		 */
		override public function onRemove():void
		{
			super.onRemove();
			facade.removeCommand( NotiEvent.ADD_NOTI );
		}
		
		/**
		 * 选择选项处理
		 * @param	e
		 */
		private function onSelectItem( e:NotiEvent ):void
		{
			var item:Item = e.data as Item;
			// 得分
			var score:int = item.score[ item.count - 1 ] ? item.score[ item.count - 1 ] : item.score[ item.score.length - 1 ];
			sendNotification( ToolsBarMediator.UPDATE_TOOLS_BAR , { score: score } );
			// 加载动画
			sendNotification( SceneEvent.MOVIE_LOAD , item.title );
			sendNotification( NotiEvent.NOTI_CHANGE , NotiEvent.CONTINUE );
			_continueInfo = item.continueValue;
			// 是否结束
			if ( item.children[ 0 ] == "win" || item.children[ 0 ] == "lose" )
			{
				_result = item.children[ 0 ];
			}
		}
		
		/**
		 * 关注通知数组
		 * @return
		 */
		override public function listNotificationInterests():Array
		{
			return [ SceneEvent.ADD_TO_SCENE , SceneEvent.MOVIE_END , NotiEvent.NOTI_CHANGE , NotiEvent.SHOW_NOTI , NotiEvent.RESTART ];
		}
		
		/**
		 * 通知处理
		 * @param	notification
		 */
		override public function handleNotification( notification:INotification ):void
		{
			super.handleNotification( notification );
			switch ( notification.getName() )
			{
				case SceneEvent.ADD_TO_SCENE: 
					var scene:DisplayObjectContainer = notification.getBody() as DisplayObjectContainer;
					if ( !scene.contains( notiBox ) )
						scene.addChild( notiBox );
					Log.trace( "++添加操作层" );
					break;
				case SceneEvent.MOVIE_END: 
					sendNotification( NotiEvent.SHOW_NOTI );
					break;
				case NotiEvent.SHOW_NOTI: 
					sendNotification( NotiEvent.ADD_NOTI , notiBox , _currentStatus );
					break;
				case NotiEvent.NOTI_CHANGE: 
					changeStatus( notification.getBody() as String );
					Log.trace( "@@更新状态: " + _currentStatus );
					break;
				case NotiEvent.RESTART: 
					notiBox.reset();
					break;
			}
		}
		
		/**
		 * 更新状态
		 * @param	v
		 */
		public function changeStatus( v:String ):void
		{
			_currentStatus = v;
		}
		
		/**
		 * 获取控制层
		 */
		protected function get notiBox():NotiFactory
		{
			return viewComponent as NotiFactory;
		}
		
		public function get continueInfo():String
		{
			return _continueInfo;
		}
		
		public function get result():String 
		{
			return _result;
		}
	
	}

}